package br.com.bus.management;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import br.com.bus.management.model.Abastecimento;
import br.com.bus.management.model.Onibus;

public class BusManagementActivity extends Activity implements View.OnClickListener, OnItemSelectedListener {
	
	private EditText kmRodado;
	private EditText litros;
	private Button adicionarInfo;
	
	Spinner busNumbers;
	Onibus bus;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        if (MainActivity.onibus.size() == 0) {
        	noBus(R.string.no_bus);
        }
        
        busNumbers = (Spinner) findViewById(R.id.options_main).
        		findViewById(R.id.numbers_spinner);
        busNumbers.setOnItemSelectedListener(this);
        loadBus();
        
        kmRodado = (EditText) findViewById(R.id.options_main)
        		.findViewById(R.id.km_editext);
        
        litros = (EditText) findViewById(R.id.options_main)
        		.findViewById(R.id.qtde_editext);
        
        adicionarInfo = (Button) findViewById(R.id.ok_button);
        adicionarInfo.setOnClickListener(this);
    }

    
	private void loadBus() {
		ArrayAdapter adp = new ArrayAdapter<Onibus>(this, android.R.layout.simple_spinner_item, MainActivity.onibus);
        adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        busNumbers.setAdapter(adp);
	}


	public void onClick(View v) {
		
		if (v.getId() == R.id.ok_button) {
			try {
				int km_rodado = Integer.parseInt(kmRodado.getText().toString());
				double litros = Double.parseDouble(this.litros.getText().toString());
				
				if (km_rodado == 0 || litros == 0) {
					unCompleteInfos(R.string.invalid_values);
					return;
				}
				
				Abastecimento abs = new Abastecimento(bus, km_rodado, litros);
				MainActivity.abastecimentos.add(abs);
				unCompleteInfos(R.string.sucessfull_infos_added);
				//Log.e("INFORMACAO: ", abs.toString());
				kmRodado.setText("");
				this.litros.setText("");
			} catch (Exception e) {
				unCompleteInfos(R.string.uncomplete_infos);
			}	
		}
		
	}
	
	
	private void unCompleteInfos(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(id)
				.setCancelable(false)
				.setNeutralButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}


	private void noBus(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(id)
				.setCancelable(false)
				.setNeutralButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
								finish();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}


	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		Toast.makeText(parent.getContext(), "Bus Number " +
		          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
		bus = MainActivity.onibus.get(pos);
	}


	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}
	
}